package sjjg.uf;

import java.io.*;

public class Traffic_Project {
    public static void main(String[] args) throws IOException {
        //获取trafficInfo缓冲输入流
        BufferedReader bufferedReader=new BufferedReader(new FileReader("src/sjjg/uf/trafficInfo.txt"));
        //获取城市个数 初始化并查集
        int cityNum =Integer.parseInt( bufferedReader.readLine());
        UF_Tree_Weight uf_tree_weight = new UF_Tree_Weight(cityNum);
        //获取已修建好的道路数量
        int hasRodes=Integer.parseInt(bufferedReader.readLine());
        //对已连接的城市进行合并
        for (int i = 0; i < hasRodes; i++) {
             //获取 两城市坐标
            String s = bufferedReader.readLine();
            String[] city = s.split(" ");
            //合并两城市
            uf_tree_weight.union(Integer.parseInt(city[0]),Integer.parseInt(city[1]));
        }
        //记录剩余组数
        int rodes = uf_tree_weight.count();
        System.out.println("还需修建"+--rodes+"条道路");
    }
}
